﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Dal;
using Model;
using System.Web.SessionState;
using easysql;
namespace Web.service
{
    /// <summary>
    /// UserService 的摘要说明
    /// </summary>
    public class UserService : BaseService<UserModel>
    {
        private UserDal dal = DalFactory.CreateUserDal();

        public void Test1(HttpContext context)
        {
            context.Response.SetCookie(new HttpCookie("a1", "a111"));
        }
        public void Test2(HttpContext context)
        {

            var c = context.Request.Cookies;
            var b = context.Response.Cookies;
        }


        public Object queryPage(HttpContext context)
        {
            PageInfo<UserModel> pi = requestPageInfo(context);
            var list = dal.QueryPageVO(pi,Restrain.Eq("del",1));
            return new { total = pi.total, rows = list };
        }
        public void add(HttpContext context)
        {
            var model = this.requestModel(context);
            model.createTime = DateTime.Now;
            if (string.IsNullOrEmpty(model.password))
            {
                model.password = "888888";
            }
            model.del = 1;
            dal.IAddOnly(model, "已有相同的用户名", Restrain.Eq("username", model.username),Restrain.Eq("del",1));
        }
        public void modify(HttpContext context)
        {
            var model = this.requestModel(context);
            model.password = null;//此处不允许修改密码
            model.username = null;//此处不允许修改用户名
            dal.IModify(model);
        }

        public void delById(HttpContext context)
        {
            UserModel user = GetSessionUserIfNotExistThrow(context);
            int id = int.Parse(context.Request["id"]);
            if (id == user.id)
            {
                throw new IException("不能删除自己");
            }
            dal.IModify(new UserModel()
            {
                id = id,
                del = 2
            });
        }
        public void login(HttpContext context)
        {
            var result = new Dictionary<string, object>();
            string username = context.Request["username"] ?? "";
            string password = context.Request["password"] ?? "";
            var user = dal.Login(username, password);
            SetSessionUser(context, user);

        }
        public Object getSessionName(HttpContext context)
        {
            UserModel user = GetSessionUserIfNotExistThrow(context);

            string name = user.nickname;
            if (string.IsNullOrEmpty(name))
            {
                name = user.username;
            }
            return new
            {
                success = true,
                entity = new {
                    id = user.id,
                    username = user.username,
                    password = "admin",
                    nickname = user.nickname
                },
                name = name,
                username = user.username
            };
        }
        public void resetPasswordByIds(HttpContext context)
        {
            String idsStr = context.Request["ids"];
            string newPassword = context.Request["newPassword"];
            if (String.IsNullOrEmpty(newPassword))
            {
                newPassword = "888888";
            }
            List<int> ids = new List<int>();
            foreach (String str in idsStr.Split(','))
            {
                ids.Add(int.Parse(str));
            }

            dal.resetPassword(ids, newPassword);
        }
        public void modifyPassword(HttpContext context)
        {
            string username = context.Request["username"] ?? "";
            string oldPassword = context.Request["oldPassword"] ?? "";
            string newPassword = context.Request["newPassword"] ?? "";
            dal.ModifyPassword(username, oldPassword, newPassword);
        }

        public object userCombobox(HttpContext context)
        {
            List<UserModel> ulist = dal.IQuery(null, Restrain.Eq("del", 1));
            List<object> list = new List<object>();
            foreach(var user in ulist)
            {
                list.Add(new
                {
                    id = user.id,
                    nickname = user.nickname,
                });
            }

            return list;
        }



      }
}